package com.itfenghuang.day15;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class A02_手撕服务器 {
    public static void main(String[] args) throws IOException {
        /**
         *  1.必须明确客户端以及服务器
         */
        //创建服务端 socket对象
        ServerSocket ss = new ServerSocket(10086);
        //监听连接
        Socket socket = ss.accept();
        //获取输入流
        InputStream is = socket.getInputStream();
        byte[] bys=new byte[1024];
        int len = is.read(bys);
        System.out.println("这是客户端的"+new String(bys,0,len));
        OutputStream os = socket.getOutputStream();
        StringBuilder content = new StringBuilder();
        //响应行
        content.append("HTTP/1.1 200 ok\r\n");
        //设置响应头   text/html: 文本
        content.append("Content-Type: text/html;charset=UTF-8\r\n");
        content.append("Server: python\r\n");
        content.append("Date: 2021-11-11 11:11:11\r\n");
        //响应头和响应体之间有个空行
        content.append("\r\n");
        content.append("你好啊,爆爆");
        os.write(new String(content).getBytes());
        os.flush();
        ss.close();
        is.close();
        ss.close();


    }
}
